/**
 * 
 */
package com.woniuxy.amusementparkos.permission.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.dto.lvd.TableDataDto;
import com.woniuxy.amusementparkos.entity.fzw.Power;
import com.woniuxy.amusementparkos.entity.fzw.Role;
import com.woniuxy.amusementparkos.exception.fzw.RoleException;
import com.woniuxy.amusementparkos.exception.fzw.RoleInUseException;
import com.woniuxy.amusementparkos.permission.Service.RoleService;
import com.woniuxy.amusementparkos.util.lzw.result.ResponseResult;

/**
 *
 * @author fzw
 *
 * 2020年4月26日
 */
@Controller
public class RoleManagementController {
	//自动注入
	@Autowired
	RoleService roleService;
	
	/**
	 * 无条件查询所有角色信息
	 * @return
	 */
	@RequestMapping("/role/selectAllRole")
	@ResponseBody
	public ResponseResult<List<Role>> selectAllRole(){
		//赋初值
		ResponseResult<List<Role>> result = new ResponseResult<List<Role>>(404,"系统错误",null);
	
		try {
			List<Role> roles=roleService.selectAllRole();
			result.setData(roles);
		} catch (RuntimeException e) {
			result.setCode(4);
			result.setMsg("查询失败！请联系工作人员！");
			return result;
		}catch (Exception e) {
			result.setCode(4);
			result.setMsg("系统错误！");
			return result;
		}
		result.setCode(200);
		result.setMsg("查询所有角色成功！");
		return result;
	}
	/**
	 * 获取一页的角色数据
	 * @param page
	 * @param limit
	 * @return
	 */
	@RequestMapping("/roleMg/getAllRole.do")
	@ResponseBody
	public TableDataDto<Role> getAllRole(@Param("page")Integer page,@Param("limit")Integer limit){
		TableDataDto<Role> res = new TableDataDto<Role>();
		try {
			//查询一页的角色信息
			List<Role> roles=roleService.selectAllRoleByPageNumber(page,limit);
			System.out.println(roles);
			//查询总条数
			Long count=roleService.selectCount();
			res.setCount(count);
			res.setData(roles);
			res.setCode(200);
			res.setMsg("获取角色数据成功");
			return res;
		} catch (RoleException e) {
			res.setMsg("角色信息处理异常，请联系管理员！");
			res.setCode(4);
			return res;
		}
		catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	
	@RequestMapping("/roleMg/insertRole")
	@ResponseBody
	public ResponseResult<Role> insertRole(Role role){
		ResponseResult<Role> res = new ResponseResult<Role>(404,"服务器错误",null);
		try {
			//插入角色信息
			roleService.insertRole(role);
			res.setCode(200);
			res.setMsg("添加成功！");
			return res;
		} catch (RoleException e) {
			res.setMsg("角色添加异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	@RequestMapping("/roleMg/updateOne")
	@ResponseBody
	public ResponseResult<Role> updateRole(Role role,@Param("powerstr")String powerstr){
		ResponseResult<Role> res = new ResponseResult<Role>(404,"服务器错误",null);
		try {
			// 调用解析方法，解析字符为权限集
			List<Power> powers = rolesHandler(powerstr);
			role.setPowers(powers);
			//插入角色信息
			roleService.updateRole(role);
			res.setCode(200);
			res.setMsg("添加成功！");
			return res;
		} catch (RoleException e) {
			res.setMsg("角色修改异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	@RequestMapping("/roleMg/deleteRoles")
	@ResponseBody
	public ResponseResult<Role> deleteRoles(int[] role_ids){
		ResponseResult<Role> res = new ResponseResult<Role>(404,"服务器错误",null);
		try {
			//删除角色信息前先检验该角色是否正被用户使用，即被从表关联
			roleService.selectRoleByRoleIds(role_ids);
			//删除角色信息
			roleService.deleteRoles(role_ids);
			res.setCode(200);
			res.setMsg("删除成功！");
			return res;
		}catch (RoleInUseException e) {
			res.setMsg(e.getMessage());
			res.setCode(4);
			return res;
		} 
		catch (RoleException e) {
			res.setMsg("角色删除异常！");
			res.setCode(4);
			return res;
		}catch (Exception e) {
			res.setMsg("系统处理异常！");
			res.setCode(4);
			return res;
		}
	}
	//解析JSON字符串转为集合
	private List<Power> rolesHandler(String powerstr) {
		// 创建结果对象
		List<Power> powser = new ArrayList<>();
		// 判断执行条件
		if(powerstr == null||powerstr.equals("")) {
			return powser;
		}
		// 拆分字符串
		String[] strs = powerstr.split(",");
		// 遍历添加赋值
		for (int i = 0; i < strs.length; i++) {
			powser.add(new Power(Integer.parseInt(strs[i]),null,null));
		}
		return powser;
	}
}
